// store/index.js

import { defineStore } from "pinia";
import createUserStore from "./user";

const delay = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(10000);
      resolve();
    }, 5000);
  });
};

export const useStore = defineStore("storeId", {
  // 注意state是一个函数
  state() {
    return {
      count: 0,
      name: "Eduardo",
    };
  },
  getters: {
    // doubleCount: (state) => state.counter * 2,
    //  也可以只直接用this访问
    doubleCount() {
      return this.count * 2;
    },
    countAddAge() {
      const userStore = createUserStore();
      return this.count + userStore.age;
    },
  },
  actions: {
    changeName(val) {
      this.name = val;
    },
    async asyncChangeCount(val) {
      await delay();
      console.log(1);
      this.count = val;
    },
  },
});
